<template>
  <!--  
    todolist思路:
    初始化: App写好数据 将数据传给List  List根据数据的个数渲染相对应的item组件
    添加功能: header组件里的input触发onchange事件 修饰符.enter  父子通信  在APP内添加数据
    删除单个: item组件里触发事件 APP自定义函数  组件通信顺序是 App -> List ->Item 
    改变单选框  : 思路和 删除单个一样
    删除已完成 : footer里触发事件 App里自定义事件 组件通信
    全选/全不选 : footer里定义计算属性 全选->小选框  小选框->全选
    已完成和全部统计个数: app定义计算属性 简单写法 只有取值功能
   -->
  <div class="todo-container">
    <div class="todo-wrap">

      <Header @add="addFn"></Header>

      <List 
        :todoList="todoList" 
        :delFn="delFn"
        :changeDone=changeDone
      ></List>

      <Footer 
        @clearFn="clearFn"
        :allCount = 'allCount'
        :trueCount = 'trueCount'
        :todoList = "todoList"
      > </Footer>

    </div>
  </div>
</template>

<script>
import Footer from './components/Footer.vue'
import Header from './components/Header.vue'
import List from './components/List.vue'
export default {
  components: { Header, List, Footer },
  data() {
    return {
      // todoList: [
      //   { id: '001', done: false, content: '吃饭' },
      //   { id: '002', done: false, content: '睡觉' },
      //   { id: '003', done: true, content: '打豆豆' }
      // ],

      todoList:JSON.parse(localStorage.getItem('todoList'))||[],
    }
  },
  methods: {
    // 添加事件
    addFn(val) {
      let id =
        this.todoList.length === 0
          ? '001'
          : this.todoList[this.todoList.length - 1].id / 1 + 1
      this.todoList.push({
        id,
        done: false,
        content: val
      })
    },
    // 删除事件
    delFn(id) {
      let ind = this.todoList.findIndex((p) => p.id === id)
      this.todoList.splice(ind, 1)
    },
    // 清除已完成
    clearFn() {
      this.todoList = this.todoList.filter((p) => p.done === false)
    },
    // 小选框状态
    changeDone(id){
      let ind = this.todoList.findIndex((p) => p.id === id)
      this.todoList[ind].done = !this.todoList[ind].done 
    },

  },
  // 计算属性
  computed:{
    allCount(){
      return  this.todoList.length
    },
    trueCount(){
     return this.todoList.filter(p => p.done === true).length
    },
  },

  watch:{
    // 开启深度监听
    todoList: {
      deep:true,
      handler(){
        // 监听list的变化 只要list变化就保存到localStorage
        localStorage.setItem('todoList',JSON.stringify(this.todoList))
      }
    }
  }


}
</script>

<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>